package real.evaluation;

import java.util.List;

import real.evaluation.set.ConstrainedSetConstruction;
import real.lang.FiniteSet;
import real.lang.RealLangException;
import real.lang.RealObject;
import real.lang.RealSet;

public class RealSetConstruction extends SyntacticalConstruction<RealSet> {
	
	private RealSet result = null;
	
	public boolean IsEvaluated() {
		return this.result != null;
	}

	/**
	 * a "such that" set construction style { x | .. }
	 * @return true if no elements listed 
	 */
	public boolean IsConstrainedConstruction()
	{
		return elements.size()==1 && elements.get(0) instanceof ConstrainedSetConstruction ;
	}
	
	private List<SyntacticalConstruction<? extends RealObject>> elements = null;	

	/**
	 * @param elements the elements to set
	 */
	public void setElements(List<SyntacticalConstruction<? extends RealObject>> elements) {
		this.elements = elements;
	}

	/**
	 * @return the elements
	 */
	public List<SyntacticalConstruction<? extends RealObject>> getElements() {
		return elements;
	}
	
	public RealSet ProperEvaluate(Evaluator eval) throws RealLangException {
		if (this.IsConstrainedConstruction()){
			ConstrainedSetConstruction conSet = (ConstrainedSetConstruction) elements.get(0);
			return (RealSet) conSet.Evaluate(eval);
		}
		FiniteSet res = new FiniteSet();
		for (SyntacticalConstruction<? extends RealObject> syn : elements)
			res.add(syn.Evaluate(eval));
		return res;
	}
}
